package com.yang.StackAndQueue;

import java.util.Deque;
import java.util.LinkedList;

public class LeetCode150 {
    public static void main(String[] args) {
        String[] tokens = {"4","3","-"};
        SolutionLeetCode150 solutionLeetCode150 = new SolutionLeetCode150();
        int i = solutionLeetCode150.evalRPN(tokens);
        System.out.println(i);
    }
}
class SolutionLeetCode150 {
    public int evalRPN(String[] tokens) {
        Deque<Integer> stack = new LinkedList<>();
        for (int i = 0; i < tokens.length; i++) {
            // 注意LeetCode 中不支持字符串使用 == 进行判断
                if (tokens[i].equals("+"))  stack.push(stack.pop() + stack.pop());
                // 注意 " — " 与 " / " 是区分前后的 按照逆波兰表达式的话 我们应该是后出栈的减去先出栈的(按照字符数组的顺序)
                else if (tokens[i].equals("-"))  stack.push(- stack.pop() + stack.pop());
                else if (tokens[i].equals("*"))  stack.push(stack.pop() * stack.pop());
                else if(tokens[i].equals("/"))  {
                    int temp1 = stack.pop();
                    int temp2 = stack.pop();
                    stack.push(temp2 / temp1);
                }
            else stack.push( Integer.valueOf(tokens[i]) );
        }
        return stack.pop();
    }
}
